const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

//分转元
const changeF2Y = (fen) => {
  var num = fen;
  num = fen * 0.01;
  num += '';
  var reg = num.indexOf('.') > -1 ? /(\d{1,3})(?=(?:\d{3})+\.)/g : /(\d{1,3})(?=(?:\d{3})+$)/g;
  num = num.replace(reg, '$1');
  num = toDecimal2(num);
  return num
};

const toDecimal2 = (x) => {
  var f = parseFloat(x);
  if (isNaN(f)) {
    return false;
  }
  var f = Math.round(x * 100) / 100;
  var s = f.toString();
  var rs = s.indexOf('.');
  if (rs < 0) {
    rs = s.length;
    s += '.';
  }
  while (s.length <= rs + 2) {
    s += '0';
  }
  return s;
}

//元转分 arg1:参数  multiple:倍数
const changeY2F = (arg1, multiple) => {
  var m = 0,
    s1 = arg1.toString(),
    s2 = multiple.toString();
  try { m += s1.split(".")[1].length } catch (e) { }
  try { m += s2.split(".")[1].length } catch (e) { }
  return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
}


// 两个浮点数求和
const Add = (num1, num2) => {
  let r1, r2, m
  try {
    r1 = `${num1}`.split('.')[1].length
  } catch (e) {
    r1 = 0
  }
  try {
    r2 = `${num2}`.split('.')[1].length
  } catch (e) {
    r2 = 0
  }
  m = Math.pow(10, Math.max(r1, r2))
  return Math.round(num1 * m + num2 * m) / m
}

// 两个浮点数相减
const Sub = (num1, num2) => {
  let r1, r2, m, n
  try {
    r1 = `${num1}`.split('.')[1].length
  } catch (e) {
    r1 = 0
  }
  try {
    r2 = `${num2}`.split('.')[1].length
  } catch (e) {
    r2 = 0
  }
  m = Math.pow(10, Math.max(r1, r2))
  n = (r1 >= r2) ? r1 : r2
  return toFixed((Math.round(num1 * m - num2 * m) / m), n)
}

// 两个浮点数相乘
const Mul = (num1, num2) => {
  let m = 0, s1 = `${num1}`, s2 = `${num2}`
  try {
    m += s1.split('.')[1].length
  } catch (e) { }
  try {
    m += s2.split('.')[1].length
  } catch (e) { }
  return Number(s1.replace('.', '')) *
    Number(s2.replace('.', '')) /
    Math.pow(10, m)
}

// 两个浮点数相除
const Div = (num1, num2) => {
  let t1, t2, r1, r2
  try {
    t1 = `${num1}`.split('.')[1].length
  } catch (e) {
    t1 = 0
  }
  try {
    t2 = `${num2}`.toString().split('.')[1].length
  } catch (e) {
    t2 = 0
  }
  r1 = Number(`${num1}`.replace('.', ''))
  r2 = Number(`${num2}`.toString().replace('.', ''))
  return (r1 / r2) * Math.pow(10, t2 - t1)
}

//生成订单号  时间戳 + 6位随机数
function orderCode() {
  var orderCode = '';
  for (var i = 0; i < 6; i++) //6位随机数，用以加在时间戳后面。
  {
    orderCode += Math.floor(Math.random() * 10);
  }
  orderCode = new Date().getTime() + orderCode;  //时间戳，用来生成订单号。
  return orderCode.toString();
}

//时间格式化
function dateFormat(fmt, date) {
  let ret;
  const opt = {
    "Y+": date.getFullYear().toString(),        // 年
    "m+": (date.getMonth() + 1).toString(),     // 月
    "d+": date.getDate().toString(),            // 日
    "H+": date.getHours().toString(),           // 时
    "M+": date.getMinutes().toString(),         // 分
    "S+": date.getSeconds().toString()          // 秒
    // 有其他格式化字符需求可以继续添加，必须转化成字符串
  };
  for (let k in opt) {
    ret = new RegExp("(" + k + ")").exec(fmt);
    if (ret) {
      fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
    };
  };
  return fmt;
}


module.exports = {
  changeF2Y: changeF2Y,
  changeY2F: changeY2F,
  Add,
  Sub,
  Mul,
  Div,
  orderCode: orderCode,
  dateFormat: dateFormat
}
